#include "configRequest.h"

char *gatewayIp;
gchar *sn=NULL;
//获得网关ip的方法
char *getGatwayIp()
{
    if (gatewayIp!=NULL)
    {
        return gatewayIp;
    }
    //1.读取当前文件目录下面的config.ini  获取 devinfo_path 地址
    GKeyFile *configiniPath = g_key_file_new();
    if (!g_key_file_load_from_file(configiniPath,"./config.ini",G_KEY_FILE_NONE,NULL))
    {
        g_warning("load config.ini error!");
        g_key_file_free(configiniPath);
        return NULL;
    }
    gchar * path = g_key_file_get_string(configiniPath,"config","devinfo_path",NULL);
    g_key_file_free(configiniPath);
    printf("1111<<<devinfo_path:%s",path);
    GKeyFile * configPath = g_key_file_new();
    if (!g_key_file_load_from_file(configiniPath,path,G_KEY_FILE_NONE,NULL))
    {
        g_warning("load config.ini error!");
        g_key_file_free(configiniPath);
        return NULL;
    }
    //拿到网关ip
    gatewayIp= g_key_file_get_string(configPath,"config","gatewayIp",NULL);
    g_key_file_free(configiniPath);
    printf("1111<<<gatewayIp:%s",gatewayIp);
    return gatewayIp;  
}

//缓存sn
gchar * getDiviceSn()
{
/*    if (sn!=NULL)
   {
      return sn;
   }
   FILE *file = fopen("/sys/class/dmi/id/product_serial","r");
   char *sn = (char*)calloc(1,256); 
   //读取一行数据
   fgets(sn,256,file);
   fclose(file);
   g_print("sn=%s\n",sn); */
   return "jfjjfjfjjfjfjfjf";
}

void * revcMsgAndSendConfig(void* param){
    while (1)
    {
        printf("wait boa  msg:\n");
        int mqId = getMsgQueue(PATHNAME_BOA_MAIN,PROCESS_ID_BOA_MAIN);//创建了一个消息队列，返回了一个消息id
        if (mqId==-1)
        {
            perror("create mq error");
            //return NULL;
        }
        Message msg={};
        int recSize = recvMsg(mqId,&msg,MSG_SIZE);
        if (recSize==-1)
        {
            perror("recv msg error");
        }
        //发送请求
        printf("接收数据成功\n");

        sendConfigRequest();

    }
}

int main(int argc, char const *argv[])
{
    //新打开一个线程用来接收mq消息 发送配置信息
    pthread_t tid;
    pthread_create(&tid,NULL,revcMsgAndSendConfig,NULL);
    //每5秒钟 发送一个心跳请求
    while(1)
    {
        sleep(5);
    }      
    return 0;
}
